// pages/personInfo/personInfo.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    sexItems: [
        {id: 1, name: "男", value: "男"},
        {id: 2, name: "女", value: "女"},
        {id: 3, name: "未选择", value: "未选择"},
    ],
    sexSelectedValue: app.globalData.userInfo.sex ? app.globalData.userInfo.sex : "未选择",
    username: app.globalData.userInfo.username,
    phone: app.globalData.userInfo.phone,
    password: "",
    wisdom: app.globalData.userInfo.profile,
    nowSex: app.globalData.userInfo.sex ? app.globalData.userInfo.sex : "未选择",
    nowUsername: app.globalData.userInfo.username,
    nowPhone: app.globalData.userInfo.phone,
    nowProfile: app.globalData.userInfo.profile,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('个人资料页面 load事件');
    this.setData({
        sexSelectedValue: app.globalData.userInfo.sex ? app.globalData.userInfo.sex : "未选择",
        username: app.globalData.userInfo.username,
        phone: app.globalData.userInfo.phone,
        wisdom: app.globalData.userInfo.profile,
        nowSex: app.globalData.userInfo.sex ? app.globalData.userInfo.sex : "未选择",
        nowUsername: app.globalData.userInfo.username,
        nowPhone: app.globalData.userInfo.phone,
        nowProfile: app.globalData.userInfo.profile,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  /*
    * 返回原页面的函数
    */
   returnMainPage () {
        wx.navigateBack({delta: 1});
    },

    /*
     * 选择性别
     */
    radioChange(e) {
        this.setData({
            sexSelectedValue: e.detail.value
        });
        console.log(this.data.sexSelectedValue);
    },

    /*
     * 用户输入昵称
     */
    usernameInputChange(e) {
        this.setData({
            username: e.detail.value
        });
        console.log(this.data.username);
    },

    /*
     * 用户输入手机号
     */
    phoneInputChange(e) {
        this.setData({
            phone: e.detail.value
        });
        console.log(this.data.phone);
    },

    /*
     * 设置密码
     */
    passwordInputChange(e) {
        this.setData({
            password: e.detail.value
        });
        console.log(this.data.password);
    },

    /*
     * 设置个性签名
     */
    wisdomChangeInput(e) {
        this.setData({
            wisdom: e.detail.value
        });
        console.log(this.data.wisdom);
    },

    /*
     * 保存个人信息
     */
    async save(e) {
        let data = {
            jwt: wx.getStorageSync('jwt')
        };
        if (this.data.username !== this.data.nowUsername) {
            data.username = this.data.username;
        }
        if (this.data.phone !== this.data.nowPhone) {
            data.phone = this.data.phone;
        }
        if (this.data.sexSelectedValue !== this.data.nowSex) {
            data.sex = this.data.sexSelectedValue;
        }
        if (this.data.wisdom !== this.data.nowProfile) {
            data.profile = this.data.wisdom;
        }
        // 输出要保存的信息
        console.log("保存的数据", data);
        // 发送到后端进行保存
        wx.request({
            url: `${app.globalData.server_addr}/users/setUserInfo`,
            method: 'POST',
            data: data,
            success: (res) => {
                console.log('更新用户信息的结果', res.data);
                wx.showToast({
                    title: '用户信息修改成功',
                    icon: 'none',
                    duration: 1000
                });
                setTimeout(this.returnMainPage, 1000);
            },
            fail: (res) => {
                console.log('更新用户信息的结果', res.data);
            },
        });
    }
});